<%@ page import="java.util.List" %>
<%@ page import="cn.demo.mvc.model.Book" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>listBook</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>

<div class="container">
    <nav class="navbar navbar-expand-sm bg-secondary navbar-dark">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link active" href="${pageContext.request.contextPath}/mvc/Book">书籍管理</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="${pageContext.request.contextPath}/mvc/Author">作者管理</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="${pageContext.request.contextPath}/mvc/BookAndAuthor">书籍-作者关系管理</a>
            </li>
        </ul>
    </nav>
    <br><br>
    <div>
        <h2>add</h2>
        <form action="Book" method="post">
            <input type="hidden" name="method" value="add">
            bookname:<input type="text" name="name" required>
            price:<input type="text" name="price" required>
            <input type="submit" value="添加书籍">
        </form>
    </div>
    <br><br>
    <div>
        <h2>update</h2>
        <form action="Book" method="post">
            <input type="hidden" name="method" value="update">
            bookID:<input type="text" name="id" required>
            bookName:<input type="text" name="name" required>
            price:<input type="text" name="price" required>
            <input type="submit" value="修改书籍">
        </form>
    </div>
    <br><br>
    <div>
        <h2>query</h2>
        <form action="Book">
            <input type="hidden" name="method" value="search">
            bookName:<input type="text" name="name">
            <input type="submit" value="根据书名查询">
        </form>
    </div>
    <br><br>
    <br><br>
    <table class="table">
        <tr>
            <th>bookID</th>
            <th>bookName</th>
            <th>bookPrice</th>
            <th>authorName</th>
            <th>管理</th>
        </tr>
        <%
            String html = "";
            List<Book> books = (List<Book>) request.getAttribute("books");
            for (Book book : books) {
                String authorName;
                authorName = book.getAuthorName();
                authorName= authorName==null?"":authorName;
                html += String.format(
                        "<tr><td>%s</td><td>%s</td><td>%s</td>"
                                + "<td><a href='./Author?method=search&name=%s'>%s</a></td>"
                                + "<td><a style='color:red' href='./Book?method=delete&id=%s'>Delete</a></td>"
                                + "</tr>",
                        String.valueOf(book.getId()),
                        book.getName(),
                        String.valueOf(book.getPrice()),
                        authorName,
                        authorName,
                        book.getId(),
                        book.getId()
                );
            }
            out.print(html);
        %>
    </table>
</div>
</body>
</html>
